const webpack = require('webpack');
const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin');

const config = {
  entry: ['babel-polyfill', './src/index.js'],
  output: {
    filename: '[name]-[hash:5]-bundle.js',
    path: path.join(__dirname, 'dist'),
    publicPath: '',
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['es2015', 'react', 'stage-0'],
            plugins: [
              ["import", { libraryName: 'antd', style: 'css'}]
            ]
          }
        },
        exclude: path.join(__dirname, 'node_modules')
      },
      {
        test: /\.css$/,
        use: [
          {loader: 'style-loader'},
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: (loader) => {
                require('autoprefixer')({
                  browsers: 'last 2 versions'
                })
              }
            }
          }
        ],
        include: path.join(__dirname, 'node_modules/antd')
        // exclude: path.join(__dirname, 'node_modules/gb-ui'),
      },
      {
        test: /\.css$/,
        use: [
          {loader: 'style-loader'},
          {
            loader: 'css-loader',
            options: {
              modules: true,
              localIdentName: '[local]_[hash:base64:5]',
              importLoaders: 1
            }
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins: (loader) => {
                require('autoprefixer')({
                  browsers: 'last 2 versions'
                })
              }
            }
          }
        ],
        exclude: path.join(__dirname, 'node_modules/antd'),
      },
      {
        test: /\.(gif|jpe?g|png|svg|)(\?.*)?$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10000,
            name: 'images/[name].[ext]'
          }
        }
      }
    ]
  },
  resolve: {
    extensions: ['.js', '.json', '.jsx'],
    alias: {
      actions: path.resolve(__dirname, 'src/actions/'),
      components: path.resolve(__dirname, 'src/components/'),
      containers: path.resolve(__dirname, 'src/containers/'),
      modules: path.resolve(__dirname, 'src/modules/'),
      reducers: path.resolve(__dirname, 'src/reducers/'),
      router: path.resolve(__dirname, 'src/router/'),
      sagas: path.resolve(__dirname, 'src/sagas/'),
      store: path.resolve(__dirname, 'src/store/'),
      ajax: path.resolve(__dirname, 'src/ajax/'),
      globals: path.resolve(__dirname, 'src/globals/'),
    }
  },
  plugins: [
    new htmlWebpackPlugin({
      template: './public/index.html'
    }),
  ],
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 2000,
    hot: true,
    historyApiFallback: true
  }
};

module.exports = config;
